package com.iyao_app.mvp.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.iyao_app.R;
import com.iyao_app.entry.Manor;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.entry.ShareData;
import com.iyao_app.entry.SignCallBack;
import com.iyao_app.entry.User;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.util.Contants;
import com.iyao_app.util.DbUtil;
import com.iyao_app.util.MarkUtil;
import com.iyaowoqu.base_library.util.LogUtil;
import com.iyaowoqu.base_library.util.PopupWindowUtils;
import com.iyaowoqu.base_library.util.Utils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/27.
 */

public class DialogSignActivity extends Activity {


    @BindView(R.id.tv_scene_name)
    TextView mTvSceneName;
    @BindView(R.id.tv_message)
    TextView mTvMessage;
    @BindView(R.id.tv_linzhu_content)
    TextView mTvLinzhuContent;
    @BindView(R.id.tv_coin)
    TextView mTvCoin;
    @BindView(R.id.tv_linzhu_count)
    TextView mTvLinzhuCount;
    @BindView(R.id.tv_daiwenzi_count)
    TextView mTvDaiwenziCount;
    @BindView(R.id.tv_qiandao_cishu_count)
    TextView mTvQiandaoCishuCount;
    @BindView(R.id.tv_daitupian_count)
    TextView mTvDaitupianCount;
    @BindView(R.id.tv_tongzhihaoyou_count)
    TextView mTvTongzhihaoyouCount;
    @BindView(R.id.sdv_head)
    SimpleDraweeView mSdvHead;
    @BindView(R.id.tv_share)
    TextView mTvShare;
    @BindView(R.id.iv_close)
    ImageView mIvClose;
    @BindView(R.id.lin_manor)
    LinearLayout mLinManor;
    @BindView(R.id.rl_root)
    RelativeLayout mlinRoot;
    @BindView(R.id.tv_sign_count)
    TextView tv_sign_count;

    private Manor manor;
    private String message1 = null;
    private String message2 = null;
    private ShareData shareUrl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_sign_activity);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        SignCallBack signCallBack = (SignCallBack) getIntent().getSerializableExtra(Contants.TYPE);
        manor = (Manor) getIntent().getSerializableExtra(Contants.OTHER_FIELD);
        mSdvHead.setImageResource(MarkUtil.getId(manor.typecode));

        if (signCallBack != null) {


            SignCallBack.LastManorUserBean lastManorUser = signCallBack.lastManorUser;
            if (lastManorUser != null) {
                User user = DbUtil.getUser();
                if (user.id.equals(lastManorUser.id)) {
                    mLinManor.setVisibility(View.VISIBLE);
                    message1 = "您是第一个在这里签到的人";
                    message2 = String.format("您在这里的签到次数已达到%d,已经成为这里的领主", signCallBack.signTime);
                } else {
                    mLinManor.setVisibility(View.VISIBLE);
                    String nickname = lastManorUser.nickname;
                    message2 = String.format("您在这里的签到次数已达到%d,代替<font color='#FF0000'>%s</font>成为这里的领主", signCallBack.signTime, nickname);
                }
            } else {
                mLinManor.setVisibility(View.GONE);
            }
            SignCallBack.LastSignUserBean lastSignUser = signCallBack.lastSignUser;
            if (lastSignUser != null) {
                if (TextUtils.isEmpty(message1)) {
                    message1 = String.format("您已经在这里签到%d次,刚刚与<font color='#FF0000'>%s</font>擦肩而过", signCallBack.signTime, lastSignUser.nickname);
                }
            }
            if (!TextUtils.isEmpty(message1))
                mTvMessage.setText(Html.fromHtml(message1));
            if (!TextUtils.isEmpty(message2))
                mTvLinzhuContent.setText(Html.fromHtml(message2));
            if (TextUtils.isEmpty(message2) && TextUtils.isEmpty(message1)) {
                message1 = String.format("您已经在这里签到%d次", signCallBack.signTime);
                mTvMessage.setText(Html.fromHtml(message1));
            }
            mTvCoin.setText(String.valueOf(signCallBack.signTotalCount));
            mTvLinzhuCount.setText(String.valueOf(signCallBack.manorCount));
            mTvDaiwenziCount.setText(String.valueOf(signCallBack.textCount));
            mTvQiandaoCishuCount.setText(String.valueOf(signCallBack.signCount));
            tv_sign_count.setText("签到" + signCallBack.signTime + "数");
            mTvDaitupianCount.setText(String.valueOf(signCallBack.imageCount));
            mTvTongzhihaoyouCount.setText(String.valueOf(signCallBack.notifyUserCount));
            mTvSceneName.setText(manor.name);
        }
    }

    @Override
    public void finish() {
        // TODO Auto-generated method stub
        super.finish();
        //关闭窗体动画显示
        this.overridePendingTransition(0, R.anim.dialog_activity_exit);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @OnClick({R.id.tv_share, R.id.iv_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_share:
                share();
                break;
            case R.id.iv_close:
                finish();
                break;
        }
    }

    private void share() {

        NetUtils.getInstance().share(DbUtil.getUser().id, "1", null, manor.sceneId, new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    if (model != null) {
                        shareUrl = model.getModel();
                        if (shareUrl != null) {
                            final PopupWindowUtils popupWindowUtils = PopupWindowUtils.getInstance();
                            popupWindowUtils.attachActivity(DialogSignActivity.this, R.layout.pop_share, mlinRoot)
                                    .wiewSize(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                                    .location(Gravity.BOTTOM, 0, Utils.getBottomStatusHeight(DialogSignActivity.this))
                                    .focuseAble(true)
                                    .Animation(PopupWindowUtils.ANIMATION_BOTTOM)
                                    .setPopupWindowItemViewClickListener(new PopupWindowUtils.SetonPopWindowItemViewClickListenter() {
                                        @Override
                                        public void onPopWindowItemViewClickListener(int id) {

                                            boolean install = UMShareAPI.get(DialogSignActivity.this).isInstall(DialogSignActivity.this, SHARE_MEDIA.WEIXIN);
                                            if (install) {
                                                UMImage umImage = new UMImage(DialogSignActivity.this, R.mipmap.logo);
                                                UMWeb web = new UMWeb(shareUrl.shareUrl);
                                                web.setTitle("“哎吆我去”邀您来玩");//标题
                                                web.setThumb(umImage);  //缩略图
                                                web.setDescription("一款好玩儿的交友平台，给你不一样的赶脚！");//描述
                                                switch (id) {
                                                    case R.id.tv_friend:

                                                        new ShareAction(DialogSignActivity.this)
                                                                .withMedia(web)
                                                                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                                                                .share();

                                                        break;
                                                    case R.id.tv_weixin:
                                                        new ShareAction(DialogSignActivity.this)
                                                                .withMedia(web)
                                                                .setCallback(umShareListener)
                                                                .setPlatform(SHARE_MEDIA.WEIXIN)
                                                                .share();
                                                        break;
                                                }
                                            } else {
                                                Toast.makeText(DialogSignActivity.this, "请先安装微信客户端", Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    }, R.id.tv_friend, R.id.tv_weixin)
                                    .show();
                        }
                    }


                }
            }

            @Override
            public void onFail(String message, int code) {

            }
        }, ShareData.class);

    }


    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            LogUtil.e("plat" + "============" + "platform" + platform);

            Toast.makeText(DialogSignActivity.this, " 分享成功啦", Toast.LENGTH_SHORT).show();
            shareUpdate();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(DialogSignActivity.this, " 分享失败啦", Toast.LENGTH_SHORT).show();
            if (t != null) {
                LogUtil.e("throw" + "============" + "throw:" + t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(DialogSignActivity.this, " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

    private void shareUpdate() {
        NetUtils.getInstance().shareUpdate(shareUrl.shareId, new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {

            }

            @Override
            public void onFail(String message, int code) {

            }
        }, null);
    }

}
